#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include "dw_qspi.h"
#include "reg_common.h"
#include "common_test.h"
#include "scu.h"
#include "mem_test.h"
#include "pcie_test.h"

//unsigned char *spi_flash_buff = 0x4040000000;

#define MEM_PATTERN_BYTE 0

#ifdef LDDDR5_CUS
    #include "lpddr5.h"
#endif

uint32_t write_read_mem(void);

int main(void)
{   
    printf("[ACPU] Boot from ACPU\r\n");

#ifdef LDDDR5_CUS

    printf("[ACPU] LPDDR5 LF_HF Init CUS. \r\n");

	lpddr5_lf_initial( PLL_FREQ_IN, TARGET_FREQ, PLL_BYPASS_EN);
	lpddr5_hf_initial( PLL_FREQ_IN, TARGET_FREQ, PLL_BYPASS_DIS, PHY_TRAINING_EN);

	lpddr5_ctrl_static_reg_config( PHY_TRAINING_EN, TARGET_FREQ);

	lpddr5_ctrl_initial_complete();

	lpddr5_ctrl_dynamic_reg_config();

    printf("[ACPU] LPDDR5 Training Freq @ %dMHz. \r\n", TARGET_FREQ);
    printf("[ACPU] LPDDR5 Training Start. \r\n");

	lpddr5_phy_training( PHY_TRAINING_EN, TARGET_FREQ, COMMON_BUS_TRAINING_EN, CK2WCK_TRAINING_EN, PHY_RD_TRAINING_EN, PHY_WR_TRAINING_EN, PHY_PRE_RD_TRAINING_EN, PHY_PRE_WR_TRAINING_EN);

    printf("[ACPU] LPDDR5 Training Done. \r\n");

	lpddr5_mrw_config( TARGET_FREQ );

	DELAY(10);

	perh_reg_wr(0x00000000,0x00800080,0x00000001,0xf);

#else

    #include "./lpddr5_js/perh_reg_lpddr5_ss.h"
    // #include "./lpddr5_js/lpddr5_final_initial.c"
	volatile unsigned int target_freq = 1600;
	volatile unsigned int pll_fin = 0x1a;
	volatile unsigned int phy_training = 0x0;
	volatile unsigned int phy_hw_cbt = 0x1;
	volatile unsigned int phy_wrlvl = 0x1;
	volatile unsigned int phy_rd = 0x1;
	volatile unsigned int phy_wr = 0x1;
	volatile unsigned int phy_prbs_rd = 0x1;	
	volatile unsigned int phy_prbs_wr = 0x1;

	volatile unsigned int pll_pass = 0x1;
	volatile unsigned int prdata_js_monitor = 0x0;

//for phy register read
	#define MAX_SIZE 1073	
    volatile int train_register[MAX_SIZE]={0x00000000,0x00000004,0x00000008,0x0000000c,0x00000010,0x00000014,0x00000018,0x0000001c,0x00000024,0x00000030,0x00000034,0x00000038,0x00000044,0x0000005c,0x00000060,0x00000064,0x00000078,0x00000080,0x00000500,0x00000504,0x00000508,0x0000050c,0x00000580,0x00000580,0x00000584,0x00000588,0x00000590,0x00000594,0x00000598,0x000005a0,0x000005a4,0x000005a8,0x000005ac,0x000005b0,0x000005b4,0x00000600,0x00000604,0x00000608,0x0000060c,0x00000610,0x00000614,0x00000650,0x00000800,0x00000804,0x00000808,0x00000a80,0x00000b00,0x00000b04,0x00000b08,0x00000b80,0x00000c00,0x00000c80,0x00000c84,0x00000c88,0x00000d00,0x00000d04,0x00000d08,0x00000d0c,0x00000d80,0x00010000,0x00010008,0x00010010,0x00010014,0x00010080,0x00010084,0x00010090,0x00010094,0x00010098,0x00010100,0x00010104,0x00010108,0x00010114,0x00010118,0x0001011c,0x00010124,0x00010128,0x00010180,0x00010184,0x0001018c,0x00010200,0x00010208,0x00010220,0x00010228,0x0001022c,0x00010280,0x00010284,0x00010288,0x0001028c,0x00010300,0x00010304,0x00010308,0x00010380,0x00010384,0x0001038c,0x00010390,0x00010394,0x00010400,0x00010404,0x00010500,0x00010508,0x00010510,0x00010514,0x00010518,0x00010520,0x00010524,0x00010580,0x00010584,0x00010600,0x00010604,0x00010608,0x0001060c,0x00010610,0x00010614,0x00010618,0x0001061c,0x00010620,0x00010624,0x00010628,0x0001062c,0x00010630,0x00010634,0x00010638,0x0001063c,0x00010640,0x00010644,0x00010648,0x0001064c,0x00010664,0x00010680,0x00010684,0x00010688,0x0001068c,0x00010690,0x00010694,0x00010698,0x0001069c,0x000106a0,0x000106a4,0x000106a8,0x000106b0,0x00010700,0x00010704,0x00010708,0x0001070c,0x00010710,0x00010780,0x00010784,0x00010788,0x0001078c,0x00010790,0x00010794,0x00010880,0x00010884,0x00010984,0x00010988,0x0001098c,0x00010990,0x00010994,0x00010998,0x00010b80,0x00010b84,0x00010b88,0x00010b8c,0x00010b90,0x00010b94,0x00010b98,0x00010b9c,0x00010ba0,0x00010ba4,0x00010c80,0x00010c84,0x00010c90,0x00010c94,0x00010c9c,0x00010ca0,0x00010ca4,0x00010d00,0x00010f00,0x00010f10,0x00010ff8,0x00010ffc,0x00011010,0x00011014,0x00011080,0x00011084,0x00011090,0x00011094,0x00011098,0x00011114,0x0001111c,0x00011124,0x00011128,0x00011180,0x00011184,0x00011284,0x0001128c,0x00011304,0x00011404,0x00011514,0x00011520,0x00011524,0x00011608,0x0001160c,0x00011610,0x00011614,0x00011618,0x0001161c,0x00011620,0x00011624,0x00011628,0x0001162c,0x00011630,0x00011634,0x00011638,0x0001163c,0x00011640,0x00011644,0x00011664,0x00011680,0x00011690,0x00011788,0x0001178c,0x00011790,0x00011794,0x00011984,0x00011988,0x0001198c,0x00011990,0x00011994,0x00011998,0x00011b84,0x00011b88,0x00011b8c,0x00011b90,0x00011b94,0x00011b98,0x00011b9c,0x00011ba0,0x00011ba4,0x00011c94,0x00011c9c,0x00011d00,0x00020000,0x00020004,0x00020008,0x00020010,0x00020014,0x00020090,0x00020094,0x00020098,0x0002009c,0x000200a0,0x000200c0,0x000200c4,0x000200e0,0x000200e4,0x000200e8,0x000200ec,0x000200f0,0x000200f4,0x000200f8,0x000200fc,0x00020100,0x00020104,0x00020108,0x0002010c,0x00020110,0x00020114,0x00030000,0x00030004,0x0003000c,0x00030010,0x00030014,0x00030018,0x0003001c,0x00030020,0x00030024,0x00030028,0x0003002c,0x00030030,0x00030080,0x00030084,0x00030088,0x01000000,0x01000004,0x01000008,0x0100000c,0x01000010,0x01000014,0x01000018,0x0100001c,0x01000020,0x01000030,0x0100003c,0x01000040,0x0100004c,0x01000050,0x01000058,0x01000060,0x01000064,0x01000068,0x0100006c,0x01000070,0x01000074,0x01000078,0x0100007c,0x01000080,0x01000084,0x01000088,0x0100008c,0x01000090,0x01000094,0x01000098,0x0100009c,0x010000a0,0x010000a4,0x010000a8,0x010000ac,0x010000b0,0x010000b4,0x010000b8,0x010000bc,0x010000c0,0x010000c4,0x010000c8,0x010000cc,0x010000d0,0x010000d4,0x010000d8,0x010000dc,0x010000e0,0x010000e4,0x010000e8,0x010000ec,0x010000f0,0x010000fc,0x01000100,0x01000110,0x01000120,0x01000130,0x01000140,0x01000150,0x01000160,0x01000164,0x01000168,0x0100016c,0x01000170,0x01000174,0x01000178,0x0100017c,0x0100018c,0x01000190,0x0100019c,0x010001a8,0x010001b4,0x010001c0,0x010001cc,0x010001d8,0x010001e4,0x010001f0,0x010001fc,0x01000208,0x01000214,0x01000220,0x0100022c,0x01000238,0x01000244,0x01000250,0x01000254,0x0100025c,0x01000268,0x01000274,0x01000280,0x0100028c,0x01000298,0x010002a4,0x010002b0,0x010002bc,0x010002c8,0x010002d4,0x010002e0,0x010002ec,0x010002f8,0x01000304,0x01000310,0x0100031c,0x01000328,0x01000334,0x01000340,0x0100034c,0x01000358,0x01000364,0x01000370,0x0100037c,0x01000388,0x01000394,0x010003a0,0x010003ac,0x010003c8,0x010003cc,0x010003d0,0x010003d4,0x010003dc,0x010003e0,0x010003ec,0x010003f8,0x01000400,0x01000410,0x0100041c,0x01000428,0x01000434,0x01000440,0x0100044c,0x01000458,0x01000464,0x01000470,0x01000474,0x01000490,0x0100049c,0x010004a8,0x010004b4,0x010004c0,0x010004cc,0x010004d8,0x010004e4,0x010004f0,0x010004f4,0x01000500,0x0100050c,0x01000518,0x01000524,0x01000530,0x0100053c,0x01000548,0x01000554,0x01000560,0x01000564,0x01000574,0x01000580,0x0100058c,0x01000590,0x01000594,0x010005e4,0x010005e8,0x010005ec,0x010005f0,0x010005f4,0x010005fc,0x01000600,0x01000604,0x01000608,0x0100060c,0x01000610,0x01000614,0x01000620,0x0100062c,0x01000638,0x01000644,0x01000650,0x01000668,0x01000680,0x01000684,0x01000688,0x0100068c,0x01000690,0x01000694,0x01000698,0x0100069c,0x010006a0,0x010006a4,0x010006a8,0x010006ac,0x010006b0,0x010006b4,0x010006b8,0x010006bc,0x01000700,0x01000704,0x01000708,0x0100070c,0x01000710,0x01000714,0x01000718,0x0100071c,0x01000720,0x01000728,0x0100072c,0x01000730,0x01000750,0x01000754,0x01000760,0x01000764,0x01000768,0x0100076c,0x01000770,0x01000774,0x01000778,0x0100077c,0x01000780,0x01000784,0x01000788,0x0100078c,0x01000790,0x01000794,0x01000798,0x0100079c,0x010007a0,0x010007a4,0x010007a8,0x010007ac,0x010007b0,0x010007c0,0x010007c4,0x010007c8,0x010007cc,0x010007d0,0x010007d4,0x010007d8,0x010007dc,0x010007e0,0x010007e4,0x010007e8,0x010007f0,0x010007f4,0x010007f8,0x010007fc,0x01000800,0x01000804,0x01000808,0x0100080c,0x01000810,0x01000814,0x01000818,0x01000820,0x01000824,0x01000828,0x0100082c,0x01000830,0x01000834,0x01000838,0x0100083c,0x01000840,0x01000850,0x01000854,0x01000858,0x0100085c,0x01000860,0x01000864,0x01000868,0x0100086c,0x01000870,0x01000880,0x01000884,0x01000888,0x0100088c,0x01000890,0x01000894,0x01000898,0x0100089c,0x010008a0,0x010008a4,0x010008b0,0x010008b4,0x010008b8,0x010008bc,0x010008c0,0x010008c4,0x010008c8,0x010008cc,0x010008d0,0x010008d4,0x010008e0,0x010008e4,0x010008e8,0x010008ec,0x010008f0,0x010008f4,0x010008f8,0x010008fc,0x01000900,0x01000904,0x0100090c,0x01000910,0x01000914,0x01000918,0x0100091c,0x01000920,0x01000924,0x01000928,0x0100092c,0x01000930,0x01000934,0x01000938,0x0100093c,0x01000940,0x01000944,0x01000948,0x0100094c,0x01000950,0x01000954,0x01000958,0x0100095c,0x01000960,0x01000964,0x01000968,0x0100096c,0x01000970,0x01000974,0x01000978,0x0100097c,0x01000980,0x01000984,0x01000988,0x0100098c,0x01000990,0x01000994,0x01000998,0x0100099c,0x010009a0,0x010009a4,0x010009e4,0x010009e8,0x010009ec,0x010009f0,0x010009f4,0x010009f8,0x010009fc,0x01000a00,0x01000a04,0x01000a08,0x01000a0c,0x01000a10,0x01000a14,0x01000a18,0x01000a1c,0x01000a20,0x01000a24,0x01000a28,0x01000a2c,0x01000a30,0x01000a34,0x01000a38,0x01000a3c,0x01000a40,0x01000a44,0x01000a48,0x01000a4c,0x01000a50,0x01000a54,0x01000a58,0x01000a5c,0x01000a60,0x01000a64,0x01000a68,0x01000a6c,0x01000a70,0x01000a74,0x01000a78,0x01000a7c,0x01000a80,0x01000a84,0x01000a88,0x01000a8c,0x01000a90,0x01000a94,0x01000a98,0x01000a9c,0x01000aa0,0x01000aa4,0x01000aa8,0x01000aac,0x01000ab0,0x01000adc,0x01000ae0,0x01000ae4,0x01000ae8,0x01000aec,0x01000af0,0x01000af4,0x01000af8,0x01000afc,0x01000b00,0x01000b04,0x01000b08,0x01000b0c,0x01000b10,0x01000b14,0x01000b18,0x01000b1c,0x01000b20,0x01000b24,0x01000b28,0x01000b2c,0x01000b30,0x01000b34,0x01000b38,0x01000b3c,0x01000b40,0x01000b44,0x01000b48,0x01000b4c,0x01000b50,0x01000b54,0x01000b58,0x01000b5c,0x01000b60,0x01000b64,0x01000b68,0x01000b6c,0x01000b70,0x01000b74,0x01000b78,0x01000b80,0x01000b84,0x01000b88,0x01000b8c,0x01000b90,0x01000b94,0x01000b98,0x01000b9c,0x01000bb0,0x01000bb4,0x01000bb8,0x01000bbc,0x01000bc0,0x01000bc4,0x01000bc8,0x01000bcc,0x01000bd0,0x01000bd8,0x01000bdc,0x01000be0,0x01000be4,0x01000bec,0x01000bf0,0x01000bf4,0x01000bf8,0x01000bfc,0x01000c00,0x01000c04,0x01000c08,0x01000c0c,0x01000c10,0x01000c14,0x01000c18,0x01000c1c,0x01000c20,0x01000c24,0x01000c28,0x01000c2c,0x01000c30,0x01000c34,0x01000c38,0x01000c3c,0x01000c40,0x01000c44,0x01000c48,0x01000c4c,0x01000c50,0x01000c54,0x01000c58,0x01000c70,0x01000c74,0x01000c78,0x01000c7c,0x01000c80,0x01000c84,0x01000c88,0x01000c8c,0x01000c90,0x01000c94,0x01000c98,0x01000c9c,0x01000ca0,0x01000ca4,0x01000ca8,0x01000cac,0x01000cb0,0x01000cb4,0x01000cb8,0x01000cbc,0x01000cc0,0x01000cc4,0x01000cc8,0x01000ccc,0x01000cd0,0x01000cd4,0x01000cd8,0x01000cdc,0x01000ce0,0x01000ce4,0x01000ce8,0x01000cec,0x01000cf0,0x01000cf4,0x01000cf8,0x01000cfc,0x01000d00,0x01000d04,0x01000d08,0x01000d0c,0x01000d10,0x01000d14,0x01000d18,0x01000d1c,0x01000d20,0x01000d24,0x01000d28,0x01000d2c,0x01000d30,0x01000d34,0x01000d38,0x01000d3c,0x01000d40,0x01000d44,0x01000d48,0x01000d4c,0x01000d50,0x01000d54,0x01000d58,0x01000d5c,0x01000d60,0x01000d64,0x01000d68,0x01000d6c,0x01000d70,0x01000d74,0x01000d78,0x01000dc0,0x01000dc4,0x01000dc8,0x01000dcc,0x01000dd0,0x01000dd4,0x01000dd8,0x01000ddc,0x01000de0,0x01000de4,0x01000de8,0x01000dec,0x01000df0,0x01000df4,0x01000df8,0x01000dfc,0x01000e00,0x01000e04,0x01000e08,0x01000e10,0x01000e14,0x01000e18,0x01000e1c,0x01000e20,0x01000e24,0x01000e28,0x01000e2c,0x01000e30,0x01000e34,0x01000e38,0x01000e3c,0x01000e40,0x01000e44,0x01000e48,0x01000e50,0x01000e54,0x01000e58,0x01000e5c,0x01000e60,0x01000e64,0x01000e68,0x01000e6c,0x01000e70,0x01000e74,0x01000e78,0x01000e7c,0x01000e80,0x01000e84,0x01000e88,0x01000e8c,0x01000e90,0x01000e94,0x01000e98,0x01000e9c,0x01000ea0,0x01000ea4,0x01000ea8,0x01000eac,0x01000eb0,0x01000eb4,0x01000eb8,0x01000ebc,0x01000ec0,0x01000ec4,0x01000ec8,0x01000ecc,0x01000ed0,0x01000ed4,0x01000ed8,0x01000edc,0x01000ee0,0x01000ee4,0x01000ee8,0x01000eec,0x01000ef0,0x01000ef4,0x01000ef8,0x01000efc,0x01000f00,0x01000f04,0x01000f08,0x01000f0c,0x01000f10,0x01000f14,0x01000f18,0x01000f1c,0x01000f20,0x01000f24,0x01000f28,0x01000f2c,0x01000f30,0x01000f34,0x01000f38,0x01000f3c,0x01000f40,0x01000f44,0x01000f48,0x01000f4c,0x01000f50,0x01000f54,0x01000f58,0x01000f5c,0x01000f60,0x01000f64,0x01000f68,0x01000f6c,0x01000f70,0x01000f74,0x01000f78,0x01000f7c,0x01000f80,0x01000f84,0x01000f88,0x01000f8c,0x01000f90,0x01000f94,0x01000f98,0x01000f9c,0x01000fa0,0x01000fa4,0x01000fa8,0x01000fac,0x01000fb0,0x01000fb4,0x01000fb8,0x01000fbc,0x01000fc0,0x01000fc4,0x01000fc8,0x01000fcc,0x01000fd0,0x01000fd4,0x01000fd8,0x01000fdc,0x01000fe0,0x01000fe4,0x01000fe8,0x01000fec,0x01000ff0,0x01000ff4,0x01000ff8,0x01000ffc,0x01001000,0x01001004,0x01001008,0x0100100c,0x01001010,0x01001014,0x01001018,0x0100101c,0x01001020,0x01001024,0x01001028,0x0100102c,0x01001030,0x01001034,0x01001038,0x0100103c,0x01001040,0x01001044,0x01001048,0x0100104c,0x01001050,0x01001054,0x01001058,0x0100105c,0x01001060,0x01001078,0x0100107c,0x01001080,0x01001084,0x01001088,0x0100108c,0x01001090,0x01001094,0x01001098,0x0100109c,0x010010a0,0x010010a4,0x010010a8,0x010010ac,0x010010b0,0x010010c0,0x010010c4,0x010010c8,0x010010cc,0x010010d0,0x010010d4,0x010010d8,0x010010dc,0x010010e0,0x010010e4,0x010010e8,0x010010ec,0x010010f0,0x010010f4,0x010010f8,0x010010fc,0x01001100,0x01001104,0x01001108,0x0100110c,0x01001110,0x01001114,0x01001118,0x0100111c,0x01001120,0x01001124,0x01001128,0x01001130,0x01001134,0x01001138,0x0100113c,0x01001140,0x01001144,0x01001148,0x0100114c,0x01001150,0x01001154,0x01001158,0x0100115c,0x01001160,0x01001164,0x01001168,0x0100116c,0x01001170,0x01001174,0x01001178,0x0100117c,0x01001180,0x01001184,0x01001188,0x0100118c,0x01001190,0x01001194,0x01001198,0x0100119c,0x01001200,0x01001204,0x01001208,0x0100120c,0x01001210,0x01001214,0x01001218,0x0100121c};
    int count = 0,i,number,addr_ptr,data_ptr;
	char line[MAX_SIZE];
	int before_reg[MAX_SIZE],after_reg[MAX_SIZE];

    printf("[ACPU] LPDDR5 LF_HF Init. \r\n");

	lpddr5_lf_initial(pll_fin,target_freq,pll_pass);
	pll_pass = 0x0;
	lpddr5_hf_initial(pll_fin,target_freq,pll_pass,phy_training);
	lpddr5_ctrl_static_reg_config(phy_training,target_freq);
	lpddr5_ctrl_initial_complete();
	lpddr5_ctrl_dynamic_reg_config();

    printf("[ACPU] LPDDR5 Training @ %d. \r\n", target_freq);
    printf("[ACPU] LPDDR5 Training Start. \r\n");
/*
    for(i=0;i<MAX_SIZE;i=i+1){
		addr_ptr = train_register[i];
		if(addr_ptr >= 0x01000000){
			before_reg[i] = perh_reg_rd(0x00000001,addr_ptr-0x01000000);
		}else{
			before_reg[i] = perh_reg_rd(0x00000000,addr_ptr);
		}
		printf("[ACPU] LPDDR5 before training register address : 0x%x, value : 0x%x\r\n",train_register[i],before_reg[i]);
	}
*/
	lpddr5_phy_training(phy_training,target_freq,phy_hw_cbt,phy_wrlvl,phy_rd,phy_wr,phy_prbs_rd,phy_prbs_wr);
/*
    for(i=0;i<MAX_SIZE;i=i+1){
		addr_ptr = train_register[i];
		if(addr_ptr >= 0x01000000){
			after_reg[i] = perh_reg_rd(0x00000001,addr_ptr-0x01000000);
		}else{
			after_reg[i] = perh_reg_rd(0x00000000,addr_ptr);
		}
		printf("[ACPU] LPDDR5 after training register address : 0x%x, value : 0x%x\r\n",train_register[i],after_reg[i]);
	}
*/    
    // fclose(after_training_file);
    printf("[ACPU] LPDDR5 Training Done. \r\n");
    printf("[ACPU] Read MR 33 value po= 0x%x.\r\n", lpddr5_mrr(0x17));
    printf("[ACPU] Read MR 34 value po= 0x%x.\r\n", lpddr5_mrr(0x18));
    // while(1);

	lpddr5_mrw_config(target_freq);
	DELAY(         10);
	perh_reg_wr(0x00000000,0x00800080,0x00000001,0xf);

#endif
    //pcie_test();

    //write_read_mem();

    // ddr_pcie_cache_en();

    int32_t ret = 0;
#if (EN_MEM_TEST_OPENSOURCE)
    // mem_test
    printf("[ACPU] ddr0 mem test start. \r\n");
	ret = mem0_test_opensource(0x0000, 0x8000);
    TEST_ASSRT(0 == ret);
    printf("[ACPU] DDR0 test end. \r\n");
		
    printf("[ACPU] ddr1 mem test start. \r\n");
	ret = mem1_test_opensource(0x0000, 0x8000);
    TEST_ASSRT(0 == ret);
    printf("[ACPU] DDR1 test end. \r\n");
		
    //qspi_flash_test();
	//flash_read_test();
	//flash_read(spi_flash_buff, 0x0, 0xb00000);
    //printf("[ACPU] read spi flash end. \r\n");
#endif

#if (EN_SRAM_TEST)
    printf("[ACPU] Sram test start. \r\n");
    // sram test
    ret = mem_test();
    TEST_ASSRT(0 == ret);
#endif

#if (EN_DDR0_TEST)
    //printf("[ACPU] DDR0 test start. \r\n");
    // ddr0 test
    //ret = ddr0_test();
    //TEST_ASSRT(0 == ret);
#endif

#if (EN_DDR1_TEST)
    printf("[ACPU] DDR1 test start. \r\n");
    // ddr1 test
    ret = ddr1_test();
    TEST_ASSRT(0 == ret);
#endif

	interrupt_plic_test();

    return 0; 
}

uint32_t write_read_mem(void)
{
    uint32_t wdata = 0; 
    uint32_t rdata = 0;
    unsigned long long addr;
    uint32_t length = 16;
    addr = TEST_START_DDR0_ADDR;
    // write32(addr + 0x0, 0x01020304);
    // write32(addr + 0x4, 0x05060708);
    // write32(addr + 0x8, 0x090A0B0C);
    // write32(addr + 0xc, 0x0D0E0FAB);    
    // printf("[ACPU] readback 0x%08x from 0x%lx\r\n",read32(addr + 0x0),addr + 0x0);
    // printf("[ACPU] readback 0x%08x from 0x%lx\r\n",read32(addr + 0x4),addr + 0x4);
    // printf("[ACPU] readback 0x%08x from 0x%lx\r\n",read32(addr + 0x8),addr + 0x8);
    // printf("[ACPU] readback 0x%08x from 0x%lx\r\n",read32(addr + 0xc),addr + 0xc);

    uint32_t temp[16] = {0};

    if(MEM_PATTERN_BYTE)
    {
        for(uint32_t i = 0; i < 16; i ++)
        {
            printf("[ACPU] readback 0x%08x from 0x%lx\r\n", read32(addr + i * 4), addr + i * 4);
        }    

        for(uint32_t i = 0; i < length; i ++)
        {
            uint32_t a;
            a = i*4;
            // temp[i] = a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24);
            // write32(addr + i * 4, a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24));
            write32(addr + i * 4, 0xaa55aa55);
        }

        for(uint32_t i = 0; i < length; i ++)
        {
            uint32_t a;
            a = i*4;
            // temp[i] = a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24);
            // printf("[ACPU] write 0x%08x, readback 0x%08x from 0x%lx\r\n", a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24), read32(addr + i * 4), addr + i * 4);
            printf("[ACPU] write 0x%08x, readback 0x%08x from 0x%lx\r\n", i , read32(addr + i * 4), addr + i * 4);
        }  
    }  

    else

    {
        for(uint32_t i = 0; i < 16; i ++)
        {
            printf("[ACPU] readback 0x%08x from 0x%lx\r\n", read32(addr + i * 4), addr + i * 4);
        }    

        for(uint32_t i = 0; i < length; i ++)
        {
            uint32_t a;
            a = i*4;
            // temp[i] = a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24);
            write32(addr + i * 4, a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24));
            // write32(addr + i * 4, i);
        }
        for(uint32_t i = 0; i < length; i ++)
        {
            uint32_t a;
            a = i*4;
            // temp[i] = a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24);
            printf("[ACPU] write 0x%08x, readback 0x%08x from 0x%lx\r\n", a + ((a + 1) << 8) + ((a + 2) << 16) + ((a + 3) << 24), read32(addr + i * 4), addr + i * 4);
            // printf("[ACPU] write 0x%08x, readback 0x%08x from 0x%lx\r\n", i , read32(addr + i * 4), addr + i * 4);
        }  
    }
    return 0;
}

